import type { App, AppContext, Component, Plugin } from 'vue'

export type SFCWithInstall<T> = T & Plugin
export type SFCInstallWithContext<T> = SFCWithInstall<T> & {
  _context: AppContext | null
}

export function withInstall<T>(comp: T) {
  (comp as SFCWithInstall<T>).install = function (app: App) {
    app.component((comp as any).name, comp as Component)
  }
  return comp as SFCWithInstall<T>
}

export function withInstallFunction<T>(fn: T, name: string) {
  (fn as SFCWithInstall<T>).install = (app: App) => {
    (fn as SFCInstallWithContext<T>)._context = app._context
    app.config.globalProperties[name] = fn
  }
  return fn as SFCInstallWithContext<T>
}
